package com.open.party.cloud.view.home.ziShenJiaoYu

import android.os.Bundle
import cn.android.x.model.LeaderUserBean
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusRecycleViewTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.view.home.ziShenJiaoYu.adapter.LeaderListAdapter
import com.open.party.cloud.viewModel.BaseViewModel
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_shadow.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*

class ZiShenJianSheLeaderListInfoActivity : AppEventBusRecycleViewTitleBaseActivity<LeaderUserBean>() {

    private var viewModel: BaseViewModel? = null
    private var adapter: LeaderListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_shadow

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_2
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("领导之窗")

        viewModel = BaseViewModel()

        initView()
        refreshData()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = LeaderListAdapter(this)
        loadingRecyclerView.adapter = adapter

        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.showLoading("努力加载中")
        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }
    }

    override fun loadData(pageVo: PageVo<LeaderUserBean>?) {
        viewModel!!.getRenCaiLeaderListData("leader")
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<ArrayList<LeaderUserBean>>) {
        if (result.eventType != "getRenCaiLeaderListData") {
            return
        }
        hideLoadingDialog()
        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    if (result.data.size == 0) {
                        loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                        loadingRecyclerView.setLoadingMoreEnabled(false)
                    } else {
                        adapter!!.setData(result.data!!)
                        loadingTipView.showContent()
                    }
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }
}